
package cg.client.gui;

import cg.client.ClientMain;
import cg.common.GameFinishedMessage;
import java.io.IOException;
import java.util.Collections;
import java.util.Map.Entry;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @param Diese Klasse beschreibt das letzte GUI, welches nach erfolgreichem Spielen einer Session angezeigt wird und die Punkte ausgibt.
 * @author Sebastian Salvisberg
 * @version 1.0
 */

public class GUIend extends javax.swing.JFrame {

    private static class window {

    }
    static final Comparator<HighScoreHelper> HIGHSCORE_ORDER = 
                                        new Comparator<HighScoreHelper>() {
            public int compare(HighScoreHelper e1, HighScoreHelper e2) {
                return e2.Punkte.compareTo(e1.Punkte);
            }
    };
    
    public GUIend(GameFinishedMessage mes) {
        initComponents();
              
        if (mes != null)
        {
            
            ArrayList<HighScoreHelper> lst = new ArrayList<HighScoreHelper>();
            lst.add(new HighScoreHelper(mes.Spieler1, mes.PunkteSpieler1));
            lst.add(new HighScoreHelper(mes.Spieler2, mes.PunkteSpieler2));
            lst.add(new HighScoreHelper(mes.Spieler3, mes.PunkteSpieler3));
            lst.add(new HighScoreHelper(mes.Spieler4, mes.PunkteSpieler4));
            
            Collections.sort(lst, HIGHSCORE_ORDER);
            
            jLSpielername1.setText(lst.get(0).Name);
            jLPunkte1.setText(lst.get(0).Punkte + " Punkte");
            jLSpielername2.setText(lst.get(1).Name);
            jLPunkte2.setText(lst.get(1).Punkte + " Punkte");
            jLSpielername3.setText(lst.get(2).Name);
            jLPunkte3.setText(lst.get(2).Punkte + " Punkte");
            jLSpielername4.setText(lst.get(3).Name);
            jLPunkte4.setText(lst.get(3).Punkte + " Punkte");
        }
    }

    

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPOben = new javax.swing.JPanel();
        jLSpielFertig = new javax.swing.JLabel();
        jLPlatz1 = new javax.swing.JLabel();
        jLPlatz2 = new javax.swing.JLabel();
        jLPlatz3 = new javax.swing.JLabel();
        jLPlatz4 = new javax.swing.JLabel();
        jLSpielername1 = new javax.swing.JLabel();
        jLSpielername2 = new javax.swing.JLabel();
        jLSpielername3 = new javax.swing.JLabel();
        jLSpielername4 = new javax.swing.JLabel();
        jLPunkte1 = new javax.swing.JLabel();
        jLPunkte2 = new javax.swing.JLabel();
        jLPunkte3 = new javax.swing.JLabel();
        jLPunkte4 = new javax.swing.JLabel();
        jBSpielBeenden = new javax.swing.JButton();
        jBNeuesSpielStarten = new javax.swing.JButton();
        jPBarGrau1 = new javax.swing.JPanel();
        jPBarGrau2 = new javax.swing.JPanel();
        jPMitte = new javax.swing.JPanel();
        jLCopyright = new javax.swing.JLabel();
        jPUnten = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("COLORETTO - ENDRESULTATE");

        jLSpielFertig.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLSpielFertig.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLSpielFertig.setText("Das Spiel ist fertig!");

        jLPlatz1.setText("1. Platz:");

        jLPlatz2.setText("2. Platz:");

        jLPlatz3.setText("3. Platz:");

        jLPlatz4.setText("4. Platz:");

        jLSpielername1.setText("Spielername");

        jLSpielername2.setText("Spielername");

        jLSpielername3.setText("Spielername");

        jLSpielername4.setText("Spielername");

        jLPunkte1.setText("Punkte");

        jLPunkte2.setText("Punkte");

        jLPunkte3.setText("Punkte");

        jLPunkte4.setText("Punkte");

        jBSpielBeenden.setText("Spiel beenden");
        jBSpielBeenden.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSpielBeendenActionPerformed(evt);
            }
        });

        jBNeuesSpielStarten.setText("Neues Spiel starten");
        jBNeuesSpielStarten.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNeuesSpielStartenActionPerformed(evt);
            }
        });

        jPBarGrau1.setBackground(new java.awt.Color(153, 153, 153));

        javax.swing.GroupLayout jPBarGrau1Layout = new javax.swing.GroupLayout(jPBarGrau1);
        jPBarGrau1.setLayout(jPBarGrau1Layout);
        jPBarGrau1Layout.setHorizontalGroup(
            jPBarGrau1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPBarGrau1Layout.setVerticalGroup(
            jPBarGrau1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jPBarGrau2.setBackground(new java.awt.Color(153, 153, 153));

        javax.swing.GroupLayout jPBarGrau2Layout = new javax.swing.GroupLayout(jPBarGrau2);
        jPBarGrau2.setLayout(jPBarGrau2Layout);
        jPBarGrau2Layout.setHorizontalGroup(
            jPBarGrau2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPBarGrau2Layout.setVerticalGroup(
            jPBarGrau2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPObenLayout = new javax.swing.GroupLayout(jPOben);
        jPOben.setLayout(jPObenLayout);
        jPObenLayout.setHorizontalGroup(
            jPObenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPObenLayout.createSequentialGroup()
                .addGap(274, 274, 274)
                .addComponent(jLSpielFertig, javax.swing.GroupLayout.PREFERRED_SIZE, 238, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(jPBarGrau1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPBarGrau2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPObenLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jBSpielBeenden, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBNeuesSpielStarten)
                .addGap(300, 300, 300))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPObenLayout.createSequentialGroup()
                .addGap(266, 266, 266)
                .addGroup(jPObenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLPlatz4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLPlatz3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLPlatz2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLPlatz1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(jPObenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLSpielername1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLSpielername2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLSpielername3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLSpielername4, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(jPObenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLPunkte3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLPunkte2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLPunkte1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLPunkte4, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPObenLayout.setVerticalGroup(
            jPObenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPObenLayout.createSequentialGroup()
                .addComponent(jPBarGrau2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(jLSpielFertig)
                .addGap(33, 33, 33)
                .addGroup(jPObenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLPlatz1)
                    .addComponent(jLSpielername1)
                    .addComponent(jLPunkte1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPObenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLPlatz2)
                    .addComponent(jLSpielername2)
                    .addComponent(jLPunkte2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPObenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLPlatz3)
                    .addComponent(jLSpielername3)
                    .addComponent(jLPunkte3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPObenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLPlatz4)
                    .addComponent(jLSpielername4)
                    .addComponent(jLPunkte4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addGroup(jPObenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBNeuesSpielStarten, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jBSpielBeenden, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPBarGrau1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPMitte.setBackground(new java.awt.Color(204, 204, 255));

        jLCopyright.setFont(new java.awt.Font("Tahoma", 0, 8)); // NOI18N
        jLCopyright.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLCopyright.setText("© Copyright by Tim Beer, Yanick Belart, Christian Friedli, Sebastian Salvisberg ");

        javax.swing.GroupLayout jPMitteLayout = new javax.swing.GroupLayout(jPMitte);
        jPMitte.setLayout(jPMitteLayout);
        jPMitteLayout.setHorizontalGroup(
            jPMitteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLCopyright, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPMitteLayout.setVerticalGroup(
            jPMitteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPMitteLayout.createSequentialGroup()
                .addGap(0, 120, Short.MAX_VALUE)
                .addComponent(jLCopyright, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPUnten.setBackground(new java.awt.Color(204, 204, 255));

        javax.swing.GroupLayout jPUntenLayout = new javax.swing.GroupLayout(jPUnten);
        jPUnten.setLayout(jPUntenLayout);
        jPUntenLayout.setHorizontalGroup(
            jPUntenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 826, Short.MAX_VALUE)
        );
        jPUntenLayout.setVerticalGroup(
            jPUntenLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 143, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPOben, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPMitte, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPUnten, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPUnten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPOben, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPMitte, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBSpielBeendenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSpielBeendenActionPerformed
         dispose(); // schliesst das Fenster
    }//GEN-LAST:event_jBSpielBeendenActionPerformed

    private void jBNeuesSpielStartenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNeuesSpielStartenActionPerformed
        GUImain gm = new GUImain();
        gm.setVisible(true);
        this.dispose(); 
    }//GEN-LAST:event_jBNeuesSpielStartenActionPerformed

    /**
     * @param args the command line arguments
     */
    
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUIend.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUIend.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUIend.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUIend.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUIend(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBNeuesSpielStarten;
    private javax.swing.JButton jBSpielBeenden;
    private javax.swing.JLabel jLCopyright;
    private javax.swing.JLabel jLPlatz1;
    private javax.swing.JLabel jLPlatz2;
    private javax.swing.JLabel jLPlatz3;
    private javax.swing.JLabel jLPlatz4;
    private javax.swing.JLabel jLPunkte1;
    private javax.swing.JLabel jLPunkte2;
    private javax.swing.JLabel jLPunkte3;
    private javax.swing.JLabel jLPunkte4;
    private javax.swing.JLabel jLSpielFertig;
    private javax.swing.JLabel jLSpielername1;
    private javax.swing.JLabel jLSpielername2;
    private javax.swing.JLabel jLSpielername3;
    private javax.swing.JLabel jLSpielername4;
    private javax.swing.JPanel jPBarGrau1;
    private javax.swing.JPanel jPBarGrau2;
    private javax.swing.JPanel jPMitte;
    private javax.swing.JPanel jPOben;
    private javax.swing.JPanel jPUnten;
    // End of variables declaration//GEN-END:variables
}
